// pages/mine/bindPhonenum/bindPhonenum.js
const app = getApp();
const util = require("../../../utils/util.js");
var interval = null; // 倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: "",
    isdisabledBtn: true,
    isdisabledBiu: false,
    // 验证码倒计时
    time: "发送验证码",
    currentTime: 61,
    authCode: "",
    num: "",
  },

  getCode: function(options) {
    let that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function() {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },


  // 获取输入的电话号码
  phoneNumInput: function(e) {
    // console.log(e.detail.value)
    let value = e.detail.value
    this.setData({
      phoneNum: value
    })

  },

  // 验证手机号
  phoneNumInputBlur: function(e) {
    let that = this
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(that.data.phoneNum))) {
      wx.showModal({
        content: '请输入正确的手机号',
        showCancel: false
      })
    } else {
      that.setData({
        // isdisabledBiu: false,
        isdisabledBtn: true
      })
    }
  },

  // 获取用户输入的验证码
  authCodeInput: function(e) {
    // console.log(e)
    this.setData({
      authCode: e.detail.value,
      isdisabledBtn: false
    })
  },

  // 发送验证码
  biuCode: function(e) {
    var that = this
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(that.data.phoneNum))) {
      wx.showModal({
        content: '请输入正确的手机号',
        showCancel: false
      })
    } else {
      this.getCode()
      that.setData({
        // isdisabledBiu: false,
        isdisabledBtn: true
      })
    }
    that.setData({
      disabled: false
    })
    // 请求验证码
    let sendCodeUrl = app.globalData.baseUrl + "weixin/sendCode"
    let data = {
      phone: that.data.phoneNum
    }
    util.requestLoading(sendCodeUrl, data, "GET").then(res => {
      // console.log(res.data.num)
      that.setData({
        num: res.data.num
      })
      wx.hideLoading()
    })
  },

  // 点击确定
  confirm: function(e) {
    let that = this
    if (that.data.num == that.data.authCode) {
      wx.showLoading({
        title: '提交中',
      })
      // 点击确认绑定手机号
      let idUrl = app.globalData.baseUrl + "weixin/wxLogin"
      let openid = wx.getStorageSync('openId')
      // console.log(openid)
      let data = {
        openid: openid,
        phone: that.data.phoneNum
      }
      util.requestLoading(idUrl, data, "GET").then(res => {
        // console.log(res)
        wx.setStorageSync('phone', that.data.phoneNum)
        if (res.data.status == 0) {
          // console.log("提交成功")
          wx.setStorageSync('cid', res.data.cid)
          wx.hideLoading()
          wx.switchTab({
            url: '../../index/index',
          })
        }
      })
    } else {
      wx.showModal({
        content: '验证码输入有误',
        showCancel: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
})